<?php

namespace App\Logic;



class MortgageLogic{
    //等额本息
    public static function debx($month,$money,$dknl){
        //$month贷款月数，20年就是240个月
        //$money;贷款总额
        if(empty($dknl)){
            $dknl  = 0.0325; //贷款年利率
        }
        //每月还款金额
        $emTotal = $money * $dknl / 12 * pow(1 + $dknl / 12, $month) / (pow(1 + $dknl / 12, $month) - 1);
        $lxTotal = 0; //总利息
        $data=[];
        for ($i = 0; $i < $month; $i++) {
            $lx   = $money*$dknl/ 12;  //每月还款利息
            $em   = $emTotal - $lx; //每月还款本金
            $money = $money - $em;
            $lxTotal = $lxTotal + $lx;
            $data[$i] = [
                'em' => $em,//每月还款本金
                'lx' => $lx,//每月还款利息
                'emTotal' => $emTotal,//每月还款金额
                'lxTotal' => $lxTotal,
                'how'=>$i+1
            ];
            return $data;
        }
    }
    //等额本金
    public static function debj($money,$month,$dknl)
    {
        //$month;贷款月数，20年就是240个月
        // $money;贷款总额
        if(empty($dknl)){
            $dknl  = 0.0325; //贷款年利率
        }
        $em   = $money / $month; //每个月还款本金
        $lxTotal = 0; //总利息
        for ($i = 0; $i < $month; $i++) {
            $lx   = $money * $dknl / 12; //每月还款利息
            $money -= $em;
            $lxTotal = $lxTotal + $lx;
        }
        $data[$i] = [
            'em' => $em,
            'lx' => $lx,
            'emTotal' => $em + $lx,
            'lxTotal' => $lxTotal,
            'how'=>$i+1
        ];
        return $data;
    }
}
